<?php
session_start();
include('config.inc.php');
// config session ให้เหมือนเข้าสู่ระบบแล้ว
// ตรวจสอบก่อนว่าเป็นผู้ดำเนินการหรือไม่
$getuser = $_GET['app_u'];
if($getuser != ""){
	$numget = mysql_num_rows(mysql_query("SELECT * FROM USERS WHERE USERNAME = '$getuser'"));
	$get = mysql_fetch_array(mysql_query("SELECT * FROM USERS WHERE USERNAME = '$getuser'"));
		if( $numget != 0){
			$_SESSION['sess_id'] = session_id();
			$_SESSION['sess_user'] = $getuser;
			$_SESSION['sess_status'] = "active";
			$_SESSION['sess_flag'] = $get['flag'];
		}
}
// sesion check
$sess_id=$_SESSION['sess_id'];
$sess_user=$_SESSION['sess_user'];
$sess_status=$_SESSION['sess_status'];
$flag = $_SESSION['sess_flag'];
if ($sess_id<>session_id() || $sess_user=="" || $sess_status<>"active") {
	header( "Location: login.php");exit();
}
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=$charset\" />";
echo "<title>$title</title>";
//#############################################################
include('runid.php');
$ac = $_GET['ac'];
?>
<html>
<head>
<title>FCR</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/script.js"></script>

</head>
<body>
<div align="center">
<?php 
echo "<div id=\"header\">
    	<div id=\"logo\">
			TT&T Public Co., Ltd.
    	</div>
        <div id=\"address\" align=\"left\">".$address."
        </div>
        <div id=\"menu\">
        	<a href=\"index.php\" id=\"on\">หน้าแรก</a>  
			<a href=\"http://web.ttt.co.th\">Intranet</a>
			<a href='logout.php'>logout</a>
        </div>
      </div>";
//<!-- Body -->
echo "<div id=\"content\" align=\"justify\"><div id='approvedetail' align='center'>	";

$app_no = $_GET['app_no'];
$app_user = $_SESSION['sess_user'];

$sql = "SELECT ap.approve_no, ap.app_day, ap.fault_no,ap.app_detail, ap.app_cost, ap.app_user,ap.reason,
d.day_in, ft.fault_name,lo.location_id, lo.site_name, d.problem,d.picture,d.solution,
un.username as un_user, un.name as un_name,un.pnthai as un_pnthai, un.unit_code as un_unit_code,un.tel as un_tel,un.fax as un_fax,un.office as un_office,
st.status_detail,
up.username as up_user, up.name as up_name,up.pnthai as up_pnthai, up.unit_code as up_unit_code,up.tel as up_tel,up.fax as up_fax,up.office as up_office,
st1.status_detail as appstatus,st1.status_id as appstatusid,
ua.username as ua_user, ua.name as ua_name,ua.pnthai as ua_pnthai, ua.unit_code as ua_unit_code,ua.tel as ua_tel,ua.fax as ua_fax,ua.office as ua_office
FROM approve ap
left join detail d on d.no = ap.fault_no
left join fault_type ft on ft.id = d.fault_type
left join location lo on lo.location_id = d.location_id
left join users un on un.username = d.username_note
left join users up on up.username = d.username_process
left join users ua on ua.username = ap.app_user
left join status_type st on st.status_id = d.status
left join status_type st1 on st1.status_id = ap.app_status
where ap.approve_no = '$app_no'";
$result = mysql_query($sql);
$num = mysql_num_rows($result);
$arr = mysql_fetch_array($result);
//การแสดงผล
if($num != 0){
?>
  <table width="889" border="1" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
        <tr>
          <td width="12%" style="vertical-align: top;padding:5px 0px 5px 10px;"><img src="images/logo_ttt.jpg" width=100 height=100></td>
        <td width="59%" valign="center"><small>
          <h2 align="center">ใบขออนุมัติการแจ้งซ่อมอุปกรณ์และสำนักงาน</h2></small></td>
          <td width="29%" valign="bottom" style="vertical-align: bottom;padding:0px 10px 0px 0px;"><br>
            <div align="right"><small><p>เลขที่&nbsp;
			        <?=$arr['approve_no'];?>
			      </p>
			    <p><?php echo thDate($arr['app_day']); ?>&nbsp;น.</p>
			    </small>
		  </div></td></tr>
        <tr>
          <td colspan="3"><hr width="98%"><br></td>
        </tr>
        <tr>
          <td colspan="3"><table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="28%" style="vertical-align: top;padding:5px 0px 5px 20px;" ><small><strong>ผู้ขออนุมัติ</strong> : <?php echo $arr['up_name']; ?></small></td>
			  <td width="24%" style="vertical-align: top;padding:5px 0px 5px 20px;"><small><strong>โทรศัพท์</strong> : <?php echo $arr['up_tel']; ?></small></td>
              <td width="19%" style="vertical-align: top;padding:5px 0px 5px 20px;"><small><strong>โทรสาร</strong> : <? echo $arr['up_fax']; ?></small></td>
              <td width="29%" style="vertical-align: top;padding:5px 0px 5px 20px;"><small><strong>อีเมลล์</strong> : <? echo $arr['up_user']."@ttt.co.th"; ?></small></td>
            </tr>
            <tr>
              <td style="vertical-align: top;padding:5px 0px 5px 20px;"><small><strong>หน่วยงาน</strong> : <? echo $arr['up_office']; ?></small></td>
              <td style="vertical-align: top;padding:5px 0px 5px 20px;"><small><strong>จังหวัด</strong> : <? echo $arr['up_pnthai']; ?></small></td>
              <td style="vertical-align: top;padding:5px 0px 5px 20px;"><small>&nbsp;</small></td>
              <td style="vertical-align: top;padding:5px 0px 5px 20px;"><small>&nbsp;</small></td>
            </tr>
            <tr>
              <td colspan="4" ><hr width="98%"></td>
            </tr>
          </table>            
            			<div style="vertical-align: top;padding:10px 0px 20px 60px;"><strong><small>รายละเอียดการแจ้งเสีย</small></strong></div>
            <table width="95%" border="1" align="center" cellpadding="0" cellspacing="0" >
              <tr>
                <td width="19%" style="vertical-align: top;padding:10px 0px 10px 10px;"><strong><small>เลขที่แจ้งเสีย</small></strong></td>
                <td width="14%" style="vertical-align: top;padding:10px 0px 10px 10px;"><strong><small>ประเภทเหตุเสีย</small></strong></td>
              <td width="22%" style="vertical-align: top;padding:10px 0px 10px 10px;"><strong><small>สถานที่</small></strong></td>
                <td width="24%" style="vertical-align: top;padding:10px 0px 10px 10px;"><strong><small>เหตุเสีย</small></strong></td>
                <td width="21%" style="vertical-align: top;padding:10px 0px 10px 10px;"><strong><small>สถานะการแจ้ง</small></strong></td>
              </tr>
              <tr>
                <td style="vertical-align: top;padding:10px 0px 10px 10px;"><small><? echo $arr['fault_no']; ?></small></td>
                <td style="vertical-align: top;padding:10px 0px 10px 10px;"><small><? echo $arr['fault_name']; ?></small></td>
              <td style="vertical-align: top;padding:10px 0px 10px 10px;"><small><? echo $arr['location_id']."-".$arr['site_name']; ?></small></td>
                <td style="vertical-align: top;padding:10px 0px 10px 10px;"><small><? echo $arr['problem']; ?></small></td>
                <td style="vertical-align: top;padding:10px 0px 10px 10px;"><small><? echo $arr['status_detail']; ?></small></td>
              </tr>
			  <tr>
			    <td colspan="5">
			      <hr width="80%">
			      <table width="100%" border="0" cellspacing="0" cellpadding="0" >
			  <tr>
                <td colspan="2" style="vertical-align: top;padding:10px 0px 10px 20px;"><strong><small>ผู้แจ้งเหตุเสีย</small></strong><small> : <? echo $arr['un_name']; ?></small></td>
                <td width="25%" style="vertical-align: top;padding:10px 0px 10px 20px;"><small><strong>โทรศัพท์</strong> : <?php echo $arr['un_tel']; ?></small></td>
                <td width="20%" style="vertical-align: top;padding:10px 0px 10px 20px;"><strong><small>โทรสาร</small></strong><small>  : <? echo $arr['un_fax']; ?></small></td>
                <td width="27%" style="vertical-align: top;padding:10px 0px 10px 20px;"><small><strong>อีเมลล์</strong>  : <? echo $arr['un_user']."@ttt.co.th"; ?></small></td>
			    </tr>
			  <tr>
                <td colspan="2" style="vertical-align: top;padding:10px 0px 10px 20px;"><small><strong>หน่วยงาน</strong> : <? echo $arr['un_office']; ?></small></td>
                <td style="vertical-align: top;padding:10px 0px 10px 20px;"><small><strong>จังหวัด</strong>  : <? echo $arr['un_pnthai']; ?></small></td>
                <td style="vertical-align: top;padding:10px 0px 10px 20px;">&nbsp;</td>
                <td style="vertical-align: top;padding:10px 0px 10px 20px;">&nbsp;</td>
			    </tr>
                </table></td>
			    </tr>
            </table>
            <div style="vertical-align: top;padding:10px 0px 20px 60px;"><small><b>การแก้ไขปัญหา :</b> <?php if($arr[solution] == ""){ echo "ยังไม่ได้รับการแก้ไข";}else{ echo $arr[solution]; } ?></small></div>
          	<div style="vertical-align: top;padding:10px 0px 20px 60px;"><small>รูปภาพประกอบ</small><br>
                <div style="padding:10px 0px 0px 30px; ">
                  <? if($arr[picture] == 'noimage.png' || $arr[picture] == ""){ 
					echo "<small><b>ไม่มีรูปประกอบ</b></small>"; 
					}else{ 
					echo "<a href=\"javascript:void(0);\" onClick=\"javascript:window.open('upload/files/$arr[picture]','_blank','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=auto, resizable=yes');\"><img src=\"upload/files/noimage.png\" border='0'></a>";
					}
					?>
                </div>
			</div>
			<hr width="98%">
			<small>
            <table width="85%" border="1" align="center" cellpadding="0" cellspacing="0">
              <tr>
                <td width="24%" style="vertical-align: top;padding:10px 0px 10px 10px;"><strong>เลขที่ขออนุมัติ</strong></td>
              <td width="37%" style="vertical-align: top;padding:10px 0px 10px 10px;"><strong>รายการ</strong></td>
                <td width="15%" style="vertical-align: top;padding:10px 0px 10px 10px;"><strong>จำนวนเงิน</strong></td>
                <td width="24%" style="vertical-align: top;padding:10px 0px 10px 10px;"><strong>สถานะการอนุมัติ</strong></td>
              </tr>
              <tr>
                <td style="vertical-align: top;padding:10px 0px 10px 10px;"><? echo $arr['approve_no']; ?>&nbsp;</td>
              <td style="vertical-align: top;padding:10px 0px 10px 10px;"><? echo $arr['app_detail']; ?>&nbsp;</td>
                <td style="vertical-align: top;padding:10px 0px 10px 10px;"><? echo $arr['app_cost']; ?> บาท &nbsp;</td>
                <td style="vertical-align: top;padding:10px 0px 10px 10px;"><? echo $arr['appstatus']; if($arr[appstatusid] == 6){ echo "<br> เนื่องจาก ".$arr[reason];}?>&nbsp;</td>
              </tr>
            </table>
            <br>
			</small>           
            <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
              <tr>
                <td width="28%" style="vertical-align: top;padding:5px 0px 5px 20px;" ><small><strong>ผู้อนุมัติ</strong> : <?php echo $arr['ua_name']; ?></small>&nbsp;</td>
                <td width="24%" style="vertical-align: top;padding:5px 0px 5px 20px;"><small><strong>โทรศัพท์</strong>  : <?php echo $arr['ua_tel']; ?></small>&nbsp;</td>
                <td width="19%" style="vertical-align: top;padding:5px 0px 5px 20px;"><small><strong>โทรสาร</strong>  : <? echo $arr['ua_fax']; ?></small>&nbsp;</td>
                <td width="29%" style="vertical-align: top;padding:5px 0px 5px 20px;"><small><strong>อีเมลล์</strong>  : <? echo $arr['ua_user']."@ttt.co.th"; ?></small>&nbsp;</td>
              </tr>
              <tr>
                <td style="vertical-align: top;padding:5px 0px 5px 20px;"><small><strong>หน่วยงาน</strong>  : <? echo $arr['ua_office']; ?></small>&nbsp;</td>
                <td style="vertical-align: top;padding:5px 0px 5px 20px;"><small><strong>จังหวัด</strong>  : <? echo $arr['ua_pnthai']; ?></small>&nbsp;</td>
                <td style="vertical-align: top;padding:5px 0px 5px 20px;"><small>&nbsp;</small></td>
                <td style="vertical-align: top;padding:5px 0px 5px 20px;"><small>&nbsp;</small></td>
              </tr>
              <tr>
                <td colspan="4" ><br>
                  </td>
              </tr>
            </table>
			<hr width="98%">
			<? 
			if($flag == 3 && ($app_user == $arr[app_user] || $ac == "app")){
					if(($arr[appstatusid] != 4 && $arr[appstatusid] != 6) && $arr[appstatusid] == 3){
						echo "<div id=\"appstatus\" align='center'>
						<small><b>บันทึกข้อความ</b></small><br><textarea name='comment' cols='40' rows='5'id='comment'></textarea><br>
						<input type='button' value='อนุมัติ' onClick=\"javascript:doApprove('a')\";></input>&nbsp;
						<input type='button' value='ไม่อนุมัติ' onClick=\"javascript:doApprove('d')\";><br>
						</div>";
					}
			}else if($flag == 2 && ($arr[status] == 4 || $arr[status] == 6)){
				echo "<br>";		
				echo "<div align='center'><a href='showfaultdetail.php?no=".$arr[fault_no]."&process=".$sess_user."' class='b-link'>ทำรายการปิดงาน</a></div>";
			}
			?>
			
			<br></td>
        </tr>
        <tr>
          <td colspan="3"> <div align="center">

          </div></td>
        </tr>
      </table>        </td>
    </tr>
  </table>
<?php 
}else{
	echo "<center><small><font color='red'>ไม่มีขอมูลเกี่ยวกับ หมายเลขการแจ้งเสียของคุณ</font><br><a href='index.php' style='text-decoration:none'>คลิกเพื่อกลับไปหน้าแรก</a></small></center>";
}
echo "</div>";
	if($app_no == '' || $ac == ''){
		echo "<span><center><br><a href='javascript:window.close();' class='b-link'>ปิดหน้าต่างนี้</a>&nbsp;<a href='index.php' class='b-link'>กลับไปหน้าแรก</a><br></center></span>";
	}
echo "</div>";

//echo "<small>$footer</small>";
mysql_close($connect);
?>
</div>

<script language="javascript">
function GetXmlHttp(){
	var HttPRequest = false;
	if(window.XMLHttpRequest) { // Mozilla, Safari,...
		HttPRequest = new XMLHttpRequest();
		if (HttPRequest.overrideMimeType) {
			HttPRequest.overrideMimeType('text/html');
		}
		return HttPRequest;
	}else if(window.ActiveXObject) { // IE
		try {
			HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
			 return HttPRequest;
	} 	  
	if (!HttPRequest) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
}
function doApprove(action){

	// เรียกค่าจากตัวแปร php
	var appno = "<?=$app_no;?>";
	var userapp = "<?=$app_user;?>";
	// เริ่ม Ajax
	//alert(appno);
	//alert(userapp);
	//alert(action);
	var xmlHttp = Inint_AJAX();
	var url = 'approve.php';
	var pmeters = "appno="+appno+"&userapp="+userapp+"&action="+action+"&reason="+encodeURI(document.getElementById("comment").value);

	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 0){document.getElementById("appstatus").innerHTML = "Loading...";}
		if(xmlHttp.readyState == 1){document.getElementById("appstatus").innerHTML = "Loading...";}
		if(xmlHttp.readyState == 2){document.getElementById("appstatus").innerHTML = "Loading...";}
		if(xmlHttp.readyState == 3){document.getElementById("appstatus").innerHTML = "Loading...";}
		if(xmlHttp.readyState == 4){
				document.getElementById("appstatus").innerHTML = xmlHttp.responseText;
		}
	};
	xmlHttp.open('POST',url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(pmeters);
}
</script>
</body>
</html>
